﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Korbit.Data;
using BTCe.Data;
using ETC;

namespace BitcoinArbitrage.Data
{
    public class OrderController
    {
        bool _Login = false;
        KorbitController _korbit;
        BTCeController _BTCe;

        public OrderController()
        {
            Initialized();
        }

        public void Initialized()
        {
            _Login = false;
            _korbit = new KorbitController();
            _BTCe = new BTCeController();
        }

        public bool OrderMaginalBuy(string strKRWBTCPrice, string strKRWBTCAmount, string strUSDBTCPrice, string strUSDBTCAmount)
        {
            bool bReturn = false;

            bReturn = OrderMagin(eOrderMagin.Buy, strKRWBTCPrice, strKRWBTCAmount, strUSDBTCPrice, strUSDBTCAmount);

            return bReturn;
        }

        public bool OrderMaginalSell(string strKRWBTCPrice, string strKRWBTCAmount, string strUSDBTCPrice, string strUSDBTCAmount)
        {
            bool bReturn = false;

            bReturn = OrderMagin(eOrderMagin.Sell, strKRWBTCPrice, strKRWBTCAmount, strUSDBTCPrice, strUSDBTCAmount);

            return bReturn;
        }

        public bool OrderMagin(eOrderMagin eOrder, string strKRWBTCPrice, string strKRWBTCAmount, string strUSDBTCPrice, string strUSDBTCAmount)
        {
            bool bReturn = false;

            if (eOrder == eOrderMagin.Buy)
            {
                _korbit.OrderBuyLimit(strKRWBTCPrice, strKRWBTCAmount);

                _BTCe.OrderSellLimit(strUSDBTCPrice, strUSDBTCAmount);
            }
            else
            {
                _korbit.OrderSellLimit(strKRWBTCPrice, strKRWBTCAmount);

                _BTCe.OrderBuyLimit(strUSDBTCPrice, strUSDBTCAmount);
            }
            
            return bReturn;
        }

        public bool OrderCancel(string strKRWOrderID, string strUSDOrderID)
        {
            bool bReturn = false;

            if (!CancelKRWOrder(strKRWOrderID))
                return bReturn;

            if(!CancelUSDOrder(strUSDOrderID))
                return bReturn;

            return true;
        }

        public bool CancelKRWOrder(string strOrderID)
        {
            bool bReturn = false;

            return bReturn;
        }

        public bool CancelUSDOrder(string strOrderID)
        {
            bool bReturn = false;

            return bReturn;
        }

        public bool Login()
        {
            if (!_korbit.lsLogin())
            {
                _korbit.Login();
            }

            return _korbit.lsLogin() ;
        }

        public bool IsLogin()
        {
            return _korbit.lsLogin();
        }

        public void RefreshData(ref DataSet dataSet)
        {
            if (!_korbit.lsLogin())
                return;

            _korbit.RefreshData();

            _BTCe.RefreshData();

            dataSet.Tables[StringConstant.TABLE_KORBIT_ASK].Clear();
            dataSet.Tables[StringConstant.TABLE_KORBIT_ASK].Merge(_korbit.GetAskTable());

            dataSet.Tables[StringConstant.TABLE_KORBIT_BID].Clear();
            dataSet.Tables[StringConstant.TABLE_KORBIT_BID].Merge(_korbit.GetBidTable());

            dataSet.Tables[StringConstant.TABLE_KORBIT_OPEN].Clear();
            dataSet.Tables[StringConstant.TABLE_KORBIT_OPEN].Merge(_korbit.GetOpenOrderTable());

            dataSet.Tables[StringConstant.TABLE_BTCE_ASK].Clear();
            dataSet.Tables[StringConstant.TABLE_BTCE_ASK].Merge(_BTCe.GetAskTable());

            dataSet.Tables[StringConstant.TABLE_BTCE_BID].Clear();
            dataSet.Tables[StringConstant.TABLE_BTCE_BID].Merge(_BTCe.GetBidTable());

            dataSet.Tables[StringConstant.TABLE_BTCE_OPEN].Clear();
            dataSet.Tables[StringConstant.TABLE_BTCE_OPEN].Merge(_BTCe.GetOpenOrderTable());
        }

        public void RefreshToken()
        {
            _korbit.RefreshToken();
        }

        public string CurrencyUSDKRW()
        {
            return ETC.CurrencyRate.CurrentRate();
        }

        public string BalanceKRW()
        {
            return _korbit.BalanceCurrency();
        }

        public string BalanceUSD()
        {
            return _BTCe.BalanceCurrency();
        }

        public string BalanceKRWBTC()
        {
            return _korbit.BalanceBTC();
        }

        public string BalanceUSDBTC()
        {
            return _BTCe.BalanceBTC();
        }

    }
}
